/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.view.gui;

import app.entities.model.DTO.MatriculaAlunoDTO;
import app.entities.model.DTO.TurmaDTO;
import app.view.classes.ViewAluno;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Murilo
 */
public class ListaAlunosMatriculados extends javax.swing.JFrame {
    
     private ViewAluno viewAluno;
     private MatriculaAlunoDTO matriculaAluno;
     private ArrayList<MatriculaAlunoDTO> ListaAlunosMatriculados;
     private JTextField edtMatricula;
     private JTextField edtNome;
     private JTextField edtSobrenoome;
     private JComboBox cbTurmas;

    public ListaAlunosMatriculados(MatriculaAlunoDTO matriculaAluno, JTextField edtMatricula,
            JTextField edtNome, JTextField edtSobrenome, JComboBox cbTurmas) {
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        pack();
        setLocation((screenSize.width/2) - (getWidth()/2), (screenSize.height/2) - (getHeight()/2));
        this.viewAluno = new ViewAluno();
        this.matriculaAluno = matriculaAluno;
        this.edtMatricula = edtMatricula;
        this.edtNome = edtNome;
        this.edtSobrenoome = edtSobrenome;
        this.cbTurmas = cbTurmas;
        this.ListaAlunosMatriculados = this.viewAluno.retornarListaAlunosMAtriculados();
        DefaultTableModel model = (DefaultTableModel) this.tblAlunosMatriculados.getModel();
        for(MatriculaAlunoDTO matAluno : this.ListaAlunosMatriculados){
            model.addRow(new Object[]{matAluno.getId(), matAluno.getCodigoMatricula(),
                            matAluno.getAluno().getNome(), matAluno.getAluno().getSobrenome()});
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblAlunosMatriculados = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lista de Alunos Matriculados");

        tblAlunosMatriculados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Matrícula", "Nome", "Sobrenome"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblAlunosMatriculados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAlunosMatriculadosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblAlunosMatriculados);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblAlunosMatriculadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAlunosMatriculadosMouseClicked
        if (tblAlunosMatriculados.getSelectedRow() != -1){
            ArrayList<TurmaDTO> listaTurmasAluno = new ArrayList<>();
            String Id = String.valueOf(((DefaultTableModel) 
                    this.tblAlunosMatriculados.getModel()).getValueAt(this.tblAlunosMatriculados.getSelectedRow(), 0));
            String Matricula = String.valueOf(((DefaultTableModel) 
                    this.tblAlunosMatriculados.getModel()).getValueAt(this.tblAlunosMatriculados.getSelectedRow(), 1));
            String Nome = String.valueOf(((DefaultTableModel) 
                    this.tblAlunosMatriculados.getModel()).getValueAt(this.tblAlunosMatriculados.getSelectedRow(), 2));
            String Sobrenome = String.valueOf(((DefaultTableModel) 
                    this.tblAlunosMatriculados.getModel()).getValueAt(this.tblAlunosMatriculados.getSelectedRow(), 3));
            this.edtMatricula.setText(Matricula);
            this.edtNome.setText(Nome);
            this.edtSobrenoome.setText(Sobrenome);
            for(MatriculaAlunoDTO matAluno : this.ListaAlunosMatriculados){
                if (Integer.valueOf(Id) == matAluno.getId()) {
                    this.matriculaAluno.setAluno(matAluno.getAluno());
                    this.matriculaAluno.setCodigoMatricula(matAluno.getCodigoMatricula());
                    this.matriculaAluno.setDataMatricula(matAluno.getDataMatricula());
                    this.matriculaAluno.setEstaAtiva(matAluno.isEstaAtiva());
                    this.matriculaAluno.setId(matAluno.getId());
                }
            }
            listaTurmasAluno = this.viewAluno.retornarListaTurmasAluno(Matricula);
            DefaultComboBoxModel cbModel = (DefaultComboBoxModel) this.cbTurmas.getModel();
            for(TurmaDTO turma : listaTurmasAluno){
                cbModel.addElement(turma.getNomeMateria());
            }
            
            this.dispose();
        }
    }//GEN-LAST:event_tblAlunosMatriculadosMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAlunosMatriculados;
    // End of variables declaration//GEN-END:variables
}
